<template>
    <div class="mine">
        <header>
            <div class="header_box">
                <div class="img">
                    <img src="@/assets/user.png" alt="" />
                </div>
                <div class="name">{{ userName }}</div>
                <div class="title">欢迎来到漫画岛</div>
            </div>
        </header>
        <div class="box">
            <div>
                <div class="item">
                    <span>阅读记录</span>
                    <span><van-icon name="arrow" /></span>
                </div>
                <div class="book" v-if="state">
                    <ul>
                        <li @click="on(i.bookid)" v-for="(i, ind) in arr" :key="ind">
                            <img :src="i.img" alt="" />
                            <span class="name">{{ i.bookname }}</span>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="collect">
                <div class="item">
                    <span>我的收藏</span><span @click="go"><van-icon name="arrow" /></span>
                </div>
            </div>
        </div>
        <div class="switch"><button @click="nologin">QUIT</button></div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            userName: "",
            arr: "",
            state: false,
        };
    },
    methods: {
        go() {
            this.$router.push({ path: "/bookrack" });
        },
        on(bookId) {
            this.$router.push({ name: "detailpage", params: { id: bookId } });
        },
        nologin() {
            localStorage.removeItem("token");
            if (localStorage.getItem("token") == null) {
                this.$router.push({ path: "/login/islogin" });
            }
        },
        //多用户不同历史记录(本地储存)
        init() {
            let user = localStorage.getItem("token");
            if (user == null) {
                return;
            }
            this.userName = user;
            this.arr = JSON.parse(localStorage.getItem(user + "history"));
            if (this.arr == null) {
                return;
            }
            this.state = true;
            // console.log(this.arr);
        },
    },
    created() {
        this.init();
    },
    beforeRouteEnter(to, from, next) {
        next((vm) => {
            // console.log(vm);
            if (localStorage.getItem("token") == null) {
                vm.$router.push({ path: "/login/islogin" });
            }
        });
    },
};
</script>

<style lang="scss" scoped>
.mine {
    header {
        z-index: -1;
        position: absolute;
        top: 0;
        width: 100%;
        height: 11.5625rem;
        background-image: url(@/assets/userbg.jpeg);
        background-size: cover;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        &::before {
            content: "";
            position: absolute;
            width: 100%;
            height: 100%;
            backdrop-filter: blur(6px);
        }
        .header_box {
            position: absolute;
            display: flex;
            flex-direction: column;
            align-items: center;
            .title {
                margin-top: 0.3125rem;
                color: #484848;
                font-size: 13px;
            }
        }
        .img {
            border-radius: 50%;
            height: 4.375rem;
            width: 4.375rem;
            img {
                width: 100%;
                height: 100%;
            }
        }
        .name {
            text-align: center;
            margin-top: 0.3125rem;
            color: #ffffff;
            font-size: 0.8125rem;
        }
    }
    .box {
        z-index: 11;
        margin-top: 10rem;
        box-sizing: border-box;
        padding: 3.125rem 1.25rem;
        width: 100%;
        min-height: 21.875rem;
        background-color: rgb(255, 255, 255);
        border-radius: 2rem 2rem 0 0;
    }
    .item {
        width: 100%;
        height: 1.875rem;
        color: #838383;
        // background-color: rgb(225, 155, 155);
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .book {
        margin-top: 0.9375rem;
        height: 6.25rem;
        width: 100%;
        ul {
            display: flex;
            width: 100%;
            overflow: auto;
        }
        li {
            margin-right: 0.625rem;
            height: 7.8125rem;
        }
        img {
            border-radius: 0.5rem;
            width: 5rem;
            height: 6.25rem;
        }
        .name {
            margin-top: 0.1875rem;
            width: 5rem;
            text-align: center;
            display: block;
            font-size: 0.75rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            color: #828282;
        }
    }
    .collect {
        margin-top: 22px;
        border-top: 1px solid #eee;
        padding-top: 0.625rem;
    }
    .switch {
        width: 100%;
        display: flex;
        justify-content: center;
        button {
            border: none;
            outline: none;
            color: white;
            background-color: #f1b2aa;
            border: 1px solid #ffc7c1;
            border-radius: 0.5rem;
            width: 9.375rem;
            height: 2.5rem;
            font-size: 0.75rem;
        }
    }
}
</style>
